--[[ 
弹头夺宝相关逻辑（交互和响应） 
]]

local M = class("DtdbCom")

local _TAG = "DTDB"

-- 添加全局事件
addGlobalEvent(_TAG, "KickOut_line")
addGlobalEvent(_TAG, "REQUEST_DATA")
addGlobalEvent(_TAG, "DEBUG_DATA")

function M:ctor()
    self:init()
end

function M:init()

end

function M:onEnter()
    if Game.subgameKey == "monster" then
        self:onMonsterEnter()
        Game.subgameKey = nil
        return
    end
    
	if DEBUG_OFFLINE then
        self:onEnterGame()
        return
    end

	netCom.send({}, slg_cmd.Dtdb.enter[1], function(pack, info)
		dump(info, "dtdb_enter")
        if checknumber(info.ret_code) == 0 then
            Game:showWaitUI(Config.localize("loading_hall"), true)
            self:onEnterGame()
        else
            Game:enterScene(ENUM.SCENCE.PLATFORM)
            Game:tipError(info.ret_code or info.code)
        end
		
    end,function()
        Game:enterScene(ENUM.SCENCE.PLATFORM)
        Game:tipMsg(Config.localize("login_is_long_time_norsp"))
    end)
end

function M:dtdbComExit()
    
end


--[[
请求数据
@param callback     function/boolean    回调函数/进入主界面
]]
function M:queryData(callback)
	local function _callback_(pack, info)
    	if pack and info then
    		--self:onGetData(pack, info)
    	end
        Game:destroyWaitUI()
        if callback == true then
            require_ex("games.dtdb.view.DtdbMainUi").new():addToScene()
        elseif type(callback) == "function" then
            callback()
        end
    end
    if not DEBUG_OFFLINE then
        _callback_()
        return
    end

end


--[[
转动请求
]]
function M:getData(type)
    netCom.send({type}, slg_cmd.Dtdb.toget[1], function(pack, info)
		dump(info, "dtdb_data")
        if checknumber(info.ret_code) == 0 then
            Game:dispatchCustomEvent(GEvent(_TAG, "REQUEST_DATA"), info)
            if not Assist.isEmpty(info.debug) then
                Game:tipMsg(info.debug)
            end
        else
            Game:tipError(info.ret_code)
        end     
    end)
end

--[[
排行榜
]]
function M:getRecordData(callback)
    --[[netCom.send({}, slg_cmd.Shz.record[1], function(pack, info)
        Game:doPluginAPI("create", "record", info)
        if callback then 
            callback()
        end
    end)--]]
end

--======================进游戏流程======================
function M:onEnterGame()
	self:queryData(true)
end

function M:onExit(op)
    netCom.send({}, slg_cmd.Dtdb.leave[1], function(pack, info)
        -- Game:tipError(info.ret_code)
    end)
end

--- 通知玩家推出游戏
function M:replyExit(pack, info)
    dump(info, _TAG)
    if checknumber(info.ret_code) > 0 then
        Game:dispatchCustomEvent(GEvent(_TAG,"KickOut_line"), info.ret_code)
        self:dtdbComExit()
    end
end

-------------------------------------
-- 狩猎年兽
function M:onMonsterEnter()
    netCom.send({}, slg_cmd.monster.enter[1], function(pack, info)
        dump(info, "monster")
        if checknumber(info.ret_code) == 0 then
            Game:destroyWaitUI()
            Game.DtdbDB:setMonsterData(info)
            require_ex("games.dtdb.view.MonsterUI").new():addToScene()
        else
            Game:enterScene(ENUM.SCENCE.PLATFORM)

            local errCode = info.ret_code or info.code
            local cfgData = Config.getConfigValue(MsgConfig, errCode) or {}
            local tip = cfgData.text or tostring(errCode)
            local y, m, d = Timer:getYMD(info.start)
            Game:tipMsg(string.format(tip, m, d))
        end
    end)
end

function M:onMonsterExit()
    netCom.send({}, slg_cmd.monster.leave[1])
end

function M:replyMonsterExit(pack, info)
    Game:dispatchCustomEvent(GEvent(_TAG,"KickOut_line"), info.ret_code)
end

function M:onMonsterExchange(id, count, callback)
    netCom.send({id, count}, slg_cmd.monster.exchange[1], function(pack, info)
        dump(info, "monster")
        if checknumber(info.ret_code) == 0 then
            Assist.showGetGoods(info.reward)
            if type(callback) == "function" then
                callback()
            end
        else
            Game:tipError(info.ret_code or info.code)
        end
    end)
end

function M:onMonsterHunting(count, callback)
    netCom.send({count}, slg_cmd.monster.hunt[1], function(pack, info)
        dump(info, "monster")
        if checknumber(info.ret_code) == 0 then
            Assist.showGetGoods(info.rewardList)
            if type(callback) == "function" then
                callback()
            end
        else
            Game:tipError(info.ret_code or info.code)
        end
    end)
end

function M:replyDebug(pack, info)
    Game:dispatchCustomEvent(GEvent(_TAG, "DEBUG_DATA"), info.msg)
end

return M:new()
